package com.jiangjiang.eshop.schedule.builder;

import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.logistics.service.LogisticsService;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import com.jiangjiang.eshop.schedule.convert.ScheduleOrderPickingItemConvert;
import com.jiangjiang.eshop.schedule.convert.ScheduleOrderSendOutDetailConvert;
import com.jiangjiang.eshop.schedule.dto.SaleDeliveryScheduleResult;
import com.jiangjiang.eshop.schedule.service.SaleDeliveryScheduler;
import com.jiangjiang.eshop.wms.constant.SaleDeliveryOrderStatus;
import com.jiangjiang.eshop.wms.dto.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 默认的销售出库单构造器
 *
 * @author zhonghuashishan
 */
@Component
@Scope("prototype")
public class DefaultSaleDeliveryOrderBuilder implements SaleDeliveryOrderBuilder {

    private SaleDeliveryOrderDTO orderDTO = new SaleDeliveryOrderDTO();

    @Autowired
    private SaleDeliveryScheduler saleDeliveryScheduler;
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private ScheduleOrderPickingItemConvert scheduleOrderPickingItemConvert;
    @Autowired
    private ScheduleOrderSendOutDetailConvert scheduleOrderSendOutDetailConvert;
    @Autowired
    private DateHelper dateHelper;


    @Override
    public SaleDeliveryOrderBuilder setOrderRelatedData(OrderInfoDTO order) {
        BeanUtils.copyProperties(order, orderDTO);
        orderDTO.setOrderId(order.getId());
        return this;
    }

    @Override
    public SaleDeliveryOrderBuilder createSaleDeliveryOrderItems(List<OrderItemDTO> items) {
        List<SaleDeliveryOrderItemDTO> saleDeliveryOrderItemList = items.stream().map(item ->
                convertSaleDeliveryOrderItem(item, saleDeliveryScheduler.getScheduleResult(item))
        ).collect(Collectors.toList());

        orderDTO.setSaleDeliveryOrderItems(saleDeliveryOrderItemList);
        return this;
    }

    /**
     * 根据调度结果组装售出库单明细DTO
     *
     * @param item   订单明细记录
     * @param result 销售调度结果
     * @return 销售出库单明细数据
     */
    private SaleDeliveryOrderItemDTO convertSaleDeliveryOrderItem(OrderItemDTO item, SaleDeliveryScheduleResult result) {
        return SaleDeliveryOrderItemDTO.builder()
                .goodsSkuId(item.getGoodsSkuId())
                .goodsSkuCode(item.getGoodsSkuCode())
                .goodsName((item.getGoodsName()))
                .saleProperties(item.getSaleProperties())
                .goodsGrossWeight(item.getGoodsGrossWeight())
                .purchaseQuantity(item.getPurchaseQuantity())
                .purchasePrice(item.getPurchasePrice())
                .promotionActivityId(item.getPromotionActivityId())
                .goodsLength(item.getGoodsLength())
                .goodsWidth(item.getGoodsWidth())
                .goodsHeight(item.getGoodsHeight())
                .pickingItems(scheduleOrderPickingItemConvert.converDTOList2SaleDeliveryPickingItems(result.getPickingItems()))
                .sendOutItems(scheduleOrderSendOutDetailConvert.convertDTOSListSaleDeliveryOrderSendOutDetailDTO(result.getSendOutDetails()))
                .build();

    }

    @Override
    public SaleDeliveryOrderBuilder createSendOutOrder(OrderInfoDTO order) {
        SendOutOrderDTO sendOutOrder = new SendOutOrderDTO();
        BeanUtils.copyProperties(order, sendOutOrder);
        sendOutOrder.setOrderId(order.getId());

        List<SendOutOrderItemDTO> sendOutOrderItems = order.getOrderItems().stream().map(orderItemDTO -> {
            SendOutOrderItemDTO sendOutOrderItemDTO = new SendOutOrderItemDTO();
            BeanUtils.copyProperties(orderItemDTO, sendOutOrderItemDTO);
            return sendOutOrderItemDTO;
        }).collect(Collectors.toList());

        sendOutOrder.setSendOutOrderItems(sendOutOrderItems);
        orderDTO.setSendOutOrderDTO(sendOutOrder);
        return this;
    }

    @Override
    public SaleDeliveryOrderBuilder createLogisticOrder(OrderInfoDTO order) {
        LogisticOrderDTO logisticOrder = logisticsService.applyLogisticOrder(order);
        orderDTO.setLogisticOrderDTO(logisticOrder);
        return this;
    }

    @Override
    public SaleDeliveryOrderBuilder initStatus() {
        orderDTO.setStatus(SaleDeliveryOrderStatus.EDITING);
        return this;
    }

    @Override
    public SaleDeliveryOrderBuilder initTimes() throws Exception {
        orderDTO.setGmtCreate(dateHelper.getCurrentDate());
        orderDTO.setGmtModified(dateHelper.getCurrentDate());
        return this;
    }

    @Override
    public SaleDeliveryOrderDTO create() {
        return orderDTO;
    }
}
